package jfoo;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicSliderUI;

import utils.FileUtil;

/*
 * it's like a progress bar, and it's seeking to the point clicked instantly
 *  inspired by http://forums.sun.com/thread.jspa?threadID=782930
 */

class Slider extends JSlider implements ChangeListener, MouseListener {
	ControlCenter controlCenter;
	boolean moving = false;
	int tw, sw, startGap;
	MySliderUI ui;

	public Slider(ControlCenter controlCenter) {
		super();
		ui = new MySliderUI(this);
		this.setUI(ui);
		tw = ui.getTrackWidth();
		sw = getWidth();
		startGap = (sw - tw) / 2;
		this.controlCenter = controlCenter;
		this.setMinimum(0);
		this.setMaximum(100);
		this.setValue(0);
		this.setBackground(Color.WHITE);
		this.setOpaque(true);
		// this.setToolTipText("fun !");
		this.addChangeListener(this);
		this.addMouseListener(this);
		// setMajorTickSpacing(20);
		// setMinorTickSpacing(10);
		// setPaintTicks(true);
		// setPaintLabels(true);

		// javax.swing.plaf.synth.SynthSliderUI
	}

	// when a song change, this is called
	public void setMaximum(int max) {
		super.setMaximum(max);
		reckon();
	}

	public void reckon() {
		tw = ui.getTrackWidth();
	}

	public void setValue(int value) {
	//	if (!getValueIsAdjusting()) {
			super.setValue(value);
		//}
	}

	public void stateChanged(ChangeEvent ce) {

	}

	public void mouseClicked(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {
		setValueIsAdjusting(true);
		setValue(getMinimum() + (getMaximum() - getMinimum()) * (e.getX() - startGap) / tw);

	}

	public void mouseReleased(MouseEvent e) {
		int a = getMinimum() + (getMaximum() - getMinimum()) * (e.getX() - startGap) / tw;
		setValue(a);
		controlCenter.processSeek(a);
		setValueIsAdjusting(false);
	}

}

/** taken from http://forum.java.sun.com/thread.jspa?threadID=568104&messageID=2806255 */
class MySliderUI extends BasicSliderUI implements MouseMotionListener, MouseListener {
	final JPopupMenu pop = new JPopupMenu();
	JMenuItem item = new JMenuItem();
	Slider slider;

	public MySliderUI(Slider slider) {
		super(slider);
		this.slider = slider;
		slider.addMouseMotionListener(this);
		slider.addMouseListener(this);
		pop.add(item);

		pop.setDoubleBuffered(true);
		pop.setFocusable(false);
		pop.setEnabled(false);
		item.setSelected(false);

		item.setFocusable(true);

		/* little hack to trigger mouse event when mouse over the item */
		item.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent me) {
				showToolTip(me);
			}
		});

	}

	public void showToolTip(MouseEvent me) {
		item.setText(FileUtil.getFormatedDuration(slider.getValue()));

		// limit the tooltip location relative to the slider
		Rectangle b = me.getComponent().getBounds();
		int x = me.getX();
		if (x < b.x) {
			x = 0;
		} else if (x > (b.width)) {
			x = b.width;
		}

		pop.show(me.getComponent(), x - 5, 30);

		item.setArmed(false);

	}

	public int getTrackWidth() {
		return (int) ((Rectangle) trackRect).getWidth();
	}

	public void mouseDragged(MouseEvent me) {
		slider.setValue(slider.getMinimum() + (slider.getMaximum() - slider.getMinimum()) * (me.getX() - slider.startGap) / slider.tw);
		showToolTip(me);
	}

	public void mouseMoved(MouseEvent me) {
	}

	public void mousePressed(MouseEvent me) {
		slider.setValue(slider.getMinimum() + (slider.getMaximum() - slider.getMinimum()) * (me.getX() - slider.startGap) / slider.tw);
		showToolTip(me);
	}

	public void mouseClicked(MouseEvent me) {
	}

	public void mouseReleased(MouseEvent me) {
		pop.setVisible(false);
	}

	public void mouseEntered(MouseEvent me) {

	}

	public void mouseExited(MouseEvent me) {

	}
}